angular.module('app.controllers')
    .controller('ProfileCtrl', [
        '$scope', '$http', 'alertService', 'accountService','fileService',
        function ($scope, $http, alertService, accountService, fileService) {
            $scope.form = { EmailConfirmed: angular.copy($scope.user.EmailConfirmed) };
            $scope.showEmailInput = false;
            $scope.verify = {
                enable: true,
                text: '发送验证码'
            };
            $scope.updateName = function() {
                if (!angular.isDefined($scope.form.newName) || $scope.form.newName === '') return;
                $http.post('/api/account?newName=' + encodeURIComponent($scope.form.newName))
                    .success(function(result) {
                        if (result.Code >= 0) {
                            var newData = { Name: $scope.form.newName };
                            accountService.updateUser(newData);
                            $scope.$emit('userUpdated', newData);
                        } else {
                            alertService.add(result.Message);
                        }
                    })
                    .error(function(x) {
                        alertService.error('更新姓名异常', x);
                    });
            };

            $scope.verifyEmail = function() {
                if ($scope.form.newEmail === $scope.user.Email) {
                    alertService.add('新Email不能与原来的相同');
                    return;
                }
                $http.post('/api/account/verify?email=' + $scope.form.newEmail)
                    .success(function(result) {
                        if (result.Code >= 0) {
                            $scope.verify.text = '已发送';
                            $scope.verify.enable = false;
                        } else {
                            alertService.add(result.Message);
                        }
                    })
                    .error(function(x) {
                        alertService.error('发送验证码异常', x);
                    });
            };

            $scope.updateEmail = function() {
                $http.post('/api/account?newEmail=' + $scope.form.newEmail + '&code=' + $scope.form.emailCode)
                    .success(function(result) {
                        if (result.Code >= 0) {
                            var newData = { Email: $scope.form.newEmail, EmailConfirmed: true };
                            $scope.form.EmailConfirmed = false;
                            $scope.form.newEmail = '';
                            $scope.form.emailCode = '';
                            accountService.updateUser(newData);
                            $scope.$emit('userUpdated', newData);
                            alertService.add('更新成功', 'success');
                        } else {
                            alertService.add(result.Message);
                        }
                    })
                    .error(function(x) {
                        alertService.error('更新Email异常', x);
                    });
            };

            $scope.changePwd = function() {
                if ($scope.form.newPwd !== $scope.form.newPwd2) {
                    alertService.add('两次输入的新密码不相同');
                    return;
                }
                if ($scope.form.newPwd === $scope.form.oldPwd) {
                    alertService.add('新旧密码不能相同');
                    return;
                }
                $http.post('/api/Account/ChangePassword', {
                    OldPassword: $scope.form.oldPwd,
                    NewPassword: $scope.form.newPwd,
                    ConfirmPassword: $scope.form.newPwd2
                }).success(function(result) {
                    if (result.Code >= 0) {
                        alertService.add('修改成功', 'success');
                        $scope.form.oldPwd = '';
                        $scope.form.newPwd = '';
                        $scope.form.newPwd2 = '';
                    } else {
                        alertService.add(result.Message);
                    }
                }).error(function(x) {
                    alertService.error('修改密码异常', x);
                });
            };

            var extAllowed = ['.jpg', '.jpeg', '.png', '.gif'];

            $scope.updateHead = function() {
                if (extAllowed.indexOf($scope.form.head.model.ext) < 0) {
                    alertService.add('文件类型不支持');
                    return;
                }
                if ($scope.form.head.model.size > 1048576) {
                    alertService.add('文件不能大于1M');
                   return;
                }

                var preview = document.getElementById('head-img-preview');
                if (preview.height !== preview.width) {
                    alertService.add('图像长宽比不是1:1');
                    return;
                }
                fileService.upload($scope.form.head.file)
                    .then(function(url) {
                        var newData = { HeadImageUrl: url };
                        accountService.updateUser(newData);
                        $scope.$emit('userUpdated', newData);
                        alertService.add('更新头像成功', 'success');
                        $scope.form.head = {};
                    }, alertService.error);
            };
        }
    ]);